#!/bin/bash

# Color
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'

# Make sure only root can run our script
[[ $EUID -ne 0 ]] && echo -e "[${red}Error${plain}] This script must be run as root!" && exit 1

echo -e "[${green}INFO${plain}] Please type instlal path(Default: /usr/share/zookeeper):"
read path
[ -z "$path" ] && path="/usr/share/zookeeper"
cd ${path}
if [ $? -ne 0 ]; then
    mkdir $path
fi
echo
echo "---------------------------"
echo "install path = ${path}"
echo "---------------------------"
echo

cd ${path}

versions=(
'3.4.9'
'3.4.10'
'3.4.11'
'3.4.12'
'3.4.13'
)

# select zk server
while true
do
echo -e "[${green}INFO${plain}] Please select version:"
for ((i=1;i<=${#versions[@]};i++ )); do
        hint="${versions[$i-1]}"
        echo -e "${green}${i}${plain}) ${hint}"
    done
read -p "Which version you'd select(Default: ${versions[0]}):" pick
    [ -z "$pick" ] && pick=1
    expr ${pick} + 1 &>/dev/null
    if [ $? -ne 0 ]; then
        echo -e "[${red}Error${plain}] Please enter a number"
        continue
    fi
    if [[ "$pick" -lt 1 || "$pick" -gt ${#versions[@]} ]]; then
        echo -e "[${red}Error${plain}] Please enter a number between 1 and ${#versions[@]}"
        continue
    fi
    version=${versions[$pick-1]}
    echo
    echo "---------------------------"
    echo "version = ${version}"
    echo "---------------------------"
    echo
break
done
tarFilePath="zookeeper-$version.tar.gz"
dirName="zookeeper-$version"

# If the tar file does not exist, download it
if [ ! -f $tarFilePath ];
then
wget  "https://archive.apache.org/dist/zookeeper/zookeeper-$version/zookeeper-$version.tar.gz"
fi

# extract the tar file if needed
if [ ! -d $dirName ];then
tar -zxvf $tarFilePath
fi


# config
echo "############################################################"
echo '#########Initialize zookeeper configuration############'
echo '###########################################################'

cp "$dirName/conf/zoo_sample.cfg" "$dirName/conf/zoo.cfg"

echo -e "[${green}INFO${plain}] Please type the data directory(Default: /var/data/zookeeper/$version):"
read dataDir
[ -z "$dataDir" ] && dataDir="/var/data/zookeeper/$version"

echo -e "[${green}INFO${plain}] Please type the log directory(Default: /var/log/zookeeper/$version):"
read logDir
[ -z "$logDir" ] && logDir="/var/log/zookeeper/$version"

echo -e "[${green}INFO${plain}] Please type the listen port(Default: 2181):"
read listenPort
[ -z "$listenPort" ] && listenPort="2181"

echo
echo "---------------------------"
echo "data directory = ${dataDir}"
echo "log directory = ${logDir}"
echo "listen port = ${listenPort}"
echo "---------------------------"
echo

sed -i "/dataDir=/c\dataDir=$dataDir" "$dirName/conf/zoo.cfg"
sed -i "/clientPort=/c\clientPort=$listenPort" "$dirName/conf/zoo.cfg"

# Ask the user if they are installed as a system service
read -p "Is it installed as a system service?yes/no(Default: yes):" pick
[ -z "$pick" ] && pick="yes"
if [ $pick == 'yes' -o $pick == 'y' ];then
 echo -e "[${green}INFO${plain}] Installing as system service..."
fi

ln -s "${path}/$dirName/bin/zkServer.sh" /etc/init.d/zkserver
systemctl daemon-reload
echo
echo "---------------------------"
echo -e "[${green}INFO${plain}] use service zkserver start for start zookeeper"
echo -e "[${green}INFO${plain}] use service zkserver start for stop zookeeper"
echo -e "[${green}INFO${plain}] use service zkserver status for get status of zookeeper server"
echo "---------------------------"
echo

service zkserver status

#echo $path =/usr/share/zookeeper

read -p "Start now?yes/no(Default: yes):" pick
[ -z "$pick" ] && pick="yes"
if [ $pick == 'yes' -o $pick == 'y' ];then
 service zkserver start
fi

read -p "Start zk-ui manager?yes/no(Default: yes):" pick
[ -z "$pick" ] && pick="yes"
if [ $pick == 'yes' -o $pick == 'y' ];then
 
 if [ ! -f "zkui-2.0-SNAPSHOT-jar-with-dependencies.jar" ];then
   wget 'https://raw.githubusercontent.com/cruldra/packages/master/zkui.zip' -O zkui.zip
   unzip zkui.zip
 fi

 java -jar zkui-2.0-SNAPSHOT-jar-with-dependencies.jar &

fi
